//
//  ViewController.m
//  YYQRScaning
//
//  Created by 蓓蕾 on 2021/8/12.
//

#import "ViewController.h"
#import "ScanViewController.h"
#import "ScanViewViewController.h"

#import "YYQRCodeUtil.h"

@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(scanAction)];
    
    UIButton *vcAction = [UIButton buttonWithType:UIButtonTypeCustom];
    vcAction.frame = CGRectMake(kScreenWidth / 2 - 50, 100, 100, 50);
    [self.view addSubview:vcAction];
    vcAction.tag = 1;
    vcAction.backgroundColor = [UIColor purpleColor];
    vcAction.layer.cornerRadius = 5;
    vcAction.layer.masksToBounds = YES;
    [vcAction addTarget:self action:@selector(actionMethod:) forControlEvents:UIControlEventTouchUpInside];
    [vcAction setTitle:@"控制器扫描器" forState:UIControlStateNormal];
    [vcAction setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    vcAction.titleLabel.font = [UIFont systemFontOfSize:15];
    
    UIButton *viewAction = [UIButton buttonWithType:UIButtonTypeCustom];
    viewAction.frame = CGRectMake(kScreenWidth / 2 - 50, 180, 100, 50);
    [self.view addSubview:viewAction];
    viewAction.tag = 2;
    viewAction.backgroundColor = [UIColor purpleColor];
    viewAction.layer.cornerRadius = 5;
    viewAction.layer.masksToBounds = YES;
    [viewAction addTarget:self action:@selector(actionMethod:) forControlEvents:UIControlEventTouchUpInside];
    [viewAction setTitle:@"视图扫描器" forState:UIControlStateNormal];
    [viewAction setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    viewAction.titleLabel.font = [UIFont systemFontOfSize:15];
    
}

-(void)scanAction
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSString *result = [YYQRCodeUtil readQRCodeFromImage:image];
    [self showAlertWithTitle:@"扫描结果" withInfo:result withCancelTitle:nil withConfirmTitle:@"确定" withResultBlock:^{
        
    }];
//    [self scanResultDetail:result];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)showAlertWithTitle:(NSString *)title withInfo:(nullable NSString *)info withCancelTitle:(nullable NSString *)cancelString withConfirmTitle:(NSString *)confirmString withResultBlock:(void(^)(void))result
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:info preferredStyle:UIAlertControllerStyleAlert];
    
    if (cancelString.length > 0) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelString style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancelAction];
    }
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (result) {
            result();
        }
    }];
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)actionMethod:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    if (tag == 1) {
        ScanViewController *scanVC = [[ScanViewController alloc] init];
        scanVC.navigationItem.title = @"扫一扫(控制器就是扫描器)";
        [self.navigationController pushViewController:scanVC animated:YES];
    }
    else
    {
        ScanViewViewController *scanVC = [[ScanViewViewController alloc] init];
        scanVC.navigationItem.title = @"扫一扫(视图就是扫描器)";
        [self.navigationController pushViewController:scanVC animated:YES];
        
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}


@end
